package org.example.dev.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.dev.common.Result;
import org.example.dev.pojo.ImageEdits;
import org.example.dev.pojo.ImageGeneration;
import org.example.dev.pojo.PageBean;
import org.example.dev.pojo.Text;
import org.example.dev.service.IImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import translate.Translate;
import org.example.dev.pojo.Image;

import java.awt.*;
import java.security.NoSuchAlgorithmException;

/**
 * 描述: 查询图片历史记录
 * @author 李佳宸
 */
@RestController
@RequestMapping("/api/image")
public class ImageController {

    @Autowired
    private IImageService imageService;

    //翻译Get
    @GetMapping("/translate")
    public String translate(@RequestParam("text") String text){
        Translate translate = new Translate();
        String result;
        try{
            result = translate.translate(text);
            System.out.println(result);
        }catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    //翻译Post
    @PostMapping("/translate")
    public String translate(@RequestBody Text text){
        Translate translate = new Translate();
        String result;
        try{
            result = translate.translate(text.getText());
            System.out.println(result);
        }catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    //返回历史记录的列表，做一个分页查询
    @GetMapping("/history")
    public Result<?> getHistory(@RequestParam(value = "currentPage", defaultValue = "1")Integer currentPage,
                                @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize
                                ){
        return Result.success(imageService.getImageGenerationsByPageId(currentPage, pageSize));
    }

    //返回历史记录的详情，根据id查询
    @GetMapping("/history/{id}")
    public ResponseEntity<ImageGeneration> getHistoryDetail(@PathVariable String id){
        ImageGeneration imageDetail = imageService.getImageDetail(id);
        if (imageDetail != null) {
            return ResponseEntity.ok(imageDetail);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); //未查询到直接返回404状态码
        }
    }

    @PostMapping("/insertImage")
    public Result<?> insertImage(@RequestBody Image image){
        imageService.insertImage(image.getImageUrl(),image.getDescription(),image.getType(), image.getWidth(), image.getHeight());
        return Result.success("插入记录成功！");
    }

    @DeleteMapping("/{id}")
    public Result<?> deleteImageGeneration(@PathVariable String id){
        imageService.deleteImageGeneration(id);
        return Result.success("删除成功！");
    }
}
